package org.jivesoftware.smack.sasl;

import de.measite.smack.Sasl;
import java.io.IOException;
import java.util.HashMap;
import org.apache.harmony.javax.security.auth.callback.Callback;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.auth.callback.NameCallback;
import org.apache.harmony.javax.security.auth.callback.PasswordCallback;
import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.harmony.javax.security.sasl.RealmCallback;
import org.apache.harmony.javax.security.sasl.RealmChoiceCallback;
import org.apache.harmony.javax.security.sasl.SaslClient;
import org.apache.harmony.javax.security.sasl.SaslException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Base64;

public abstract class SASLMechanism
  implements CallbackHandler
{
  protected String authenticationId;
  protected String hostname;
  protected String password;
  private SASLAuthentication saslAuthentication;
  protected SaslClient sc;

  public SASLMechanism(SASLAuthentication paramSASLAuthentication)
  {
    this.saslAuthentication = paramSASLAuthentication;
  }

  protected void authenticate()
    throws IOException, XMPPException
  {
    Object localObject = null;
    try
    {
      if (this.sc.hasInitialResponse())
      {
        String str = Base64.encodeBytes(this.sc.evaluateChallenge(new byte[0]), 8);
        localObject = str;
      }
      getSASLAuthentication().send(new AuthMechanism(this, getName(), localObject));
      return;
    }
    catch (SaslException localSaslException)
    {
      throw new XMPPException("SASL authentication failed", localSaslException);
    }
  }

  public void authenticate(String paramString1, String paramString2, String paramString3)
    throws IOException, XMPPException
  {
    this.authenticationId = paramString1;
    this.password = paramString3;
    this.hostname = paramString2;
    String[] arrayOfString = new String[1];
    arrayOfString[0] = getName();
    this.sc = Sasl.createSaslClient(arrayOfString, paramString1, "xmpp", paramString2, new HashMap(), this);
    authenticate();
  }

  public void authenticate(String paramString1, String paramString2, CallbackHandler paramCallbackHandler)
    throws IOException, XMPPException
  {
    String[] arrayOfString = new String[1];
    arrayOfString[0] = getName();
    this.sc = Sasl.createSaslClient(arrayOfString, paramString1, "xmpp", paramString2, new HashMap(), paramCallbackHandler);
    authenticate();
  }

  public void challengeReceived(String paramString)
    throws IOException
  {
    byte[] arrayOfByte;
    Response localResponse;
    if (paramString != null)
    {
      arrayOfByte = this.sc.evaluateChallenge(Base64.decode(paramString));
      if (arrayOfByte != null)
        break label56;
      localResponse = new Response(this);
    }
    while (true)
    {
      while (true)
      {
        getSASLAuthentication().send(localResponse);
        return;
        arrayOfByte = this.sc.evaluateChallenge(new byte[0]);
      }
      label56: localResponse = new Response(this, Base64.encodeBytes(arrayOfByte, 8));
    }
  }

  protected abstract String getName();

  protected SASLAuthentication getSASLAuthentication()
  {
    return this.saslAuthentication;
  }

  public void handle(Callback[] paramArrayOfCallback)
    throws IOException, UnsupportedCallbackException
  {
    int i = 0;
    if (i >= paramArrayOfCallback.length)
      return;
    if (paramArrayOfCallback[i] instanceof NameCallback)
      ((NameCallback)paramArrayOfCallback[i]).setName(this.authenticationId);
    do
      while (true)
      {
        while (true)
        {
          while (true)
            ++i;
          if (!(paramArrayOfCallback[i] instanceof PasswordCallback))
            break;
          ((PasswordCallback)paramArrayOfCallback[i]).setPassword(this.password.toCharArray());
        }
        if (!(paramArrayOfCallback[i] instanceof RealmCallback))
          break;
        ((RealmCallback)paramArrayOfCallback[i]).setText(this.hostname);
      }
    while (paramArrayOfCallback[i] instanceof RealmChoiceCallback);
    throw new UnsupportedCallbackException(paramArrayOfCallback[i]);
  }

  public class AuthMechanism extends Packet
  {
    private final String authenticationText;
    private final String name;

    public AuthMechanism(, String paramString1, String paramString2)
    {
      if (paramString1 == null)
        throw new NullPointerException("SASL mechanism name shouldn't be null.");
      this.name = paramString1;
      this.authenticationText = paramString2;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<auth mechanism=\"").append(this.name);
      localStringBuilder.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.authenticationText != null) && (this.authenticationText.trim().length() > 0))
        localStringBuilder.append(this.authenticationText);
      localStringBuilder.append("</auth>");
      return localStringBuilder.toString();
    }
  }

  public static class Challenge extends Packet
  {
    private final String data;

    public Challenge(String paramString)
    {
      this.data = paramString;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<challenge xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.data != null) && (this.data.trim().length() > 0))
        localStringBuilder.append(this.data);
      localStringBuilder.append("</challenge>");
      return localStringBuilder.toString();
    }
  }

  public static class Failure extends Packet
  {
    private final String condition;

    public Failure(String paramString)
    {
      this.condition = paramString;
    }

    public String getCondition()
    {
      return this.condition;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.condition != null) && (this.condition.trim().length() > 0))
        localStringBuilder.append("<").append(this.condition).append("/>");
      localStringBuilder.append("</failure>");
      return localStringBuilder.toString();
    }
  }

  public class Response extends Packet
  {
    private final String authenticationText;

    public Response()
    {
      this.authenticationText = null;
    }

    public Response(, String paramString)
    {
      if ((paramString == null) || (paramString.trim().length() == 0))
        this.authenticationText = null;
      while (true)
      {
        return;
        this.authenticationText = paramString;
      }
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if (this.authenticationText != null)
        localStringBuilder.append(this.authenticationText);
      while (true)
      {
        localStringBuilder.append("</response>");
        return localStringBuilder.toString();
        localStringBuilder.append("=");
      }
    }
  }

  public static class Success extends Packet
  {
    private final String data;

    public Success(String paramString)
    {
      this.data = paramString;
    }

    public String toXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
      if ((this.data != null) && (this.data.trim().length() > 0))
        localStringBuilder.append(this.data);
      localStringBuilder.append("</success>");
      return localStringBuilder.toString();
    }
  }
}